<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午2:07
 */


namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class FullScreen extends Base
{

    public $table = 'system_full_screen';
    private $cacheSystemFullScreenHash = 'CACHE:SYSTEM:FULLSCREEN:HASH';


    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    public function getFullPage(){
        $data =
        [
            0 => '扩列页面',
            1 => '动态页面',
            2 => '娱乐页面',
            3 => '消息页面',
            4 => '我的页面',
            5 => '房间',
            6 => '其他页面',
        ];

        return $data;
    }


    /**
     * 获取全服飘屏信息
     * @param $id
     */
    public function getFullScreen($id){
        $cacheData = Redis::HGET($this->cacheSystemFullScreenHash, $id);

        if (!empty($cacheData)){
            return json_decode($cacheData, true);
        }

        $info = $this -> find($id);
        if ($info){
            $info['bg_img_url'] = $info['bg_img_url'] ? getPicDomain() . $info['bg_img_url'] : '';
            $info['action_img_url'] = $info['action_img_url'] ? getPicDomain() . $info['action_img_url'] : '';

            Redis::HSET($this->cacheSystemFullScreenHash, $info['id'], json_encode($info));
            Redis::EXPIRE($this->cacheSystemFullScreenHash, 3600);
        }

        return (array)$info;
    }

    public function add($data)
    {
        return DB::table($this->table)->insert($data);
    }

    public function editById($id, $data)
    {
        $data['edit_time'] = time();
        $res = DB::table($this->table)->where('id', $id)->update($data);
        $this -> cleanCache($id);
        return $res;
    }

    public function del($id)
    {
        $res = DB::table($this->table)->delete($id);
        $this -> cleanCache($id);
        return $res;
    }

    public function find($id)
    {
        $info = DB::table($this->table)->find($id);

        return $this->objToArray($info);
    }


    function fullList()
    {
        $result = DB::table($this->table)->orderBy('id', 'desc')->get();
        $result = $this->objToArray($result);
        foreach ($result as $k => $v){
            if (!empty($v['bg_img_url'])){
                $result[$k]['bg_img'] = $v['bg_img_url'];
                $result[$k]['bg_img_url'] = getPicDomain() . $v['bg_img_url'];
            }
            if (!empty($v['action_img_url'])){
                $result[$k]['action_img'] = $v['action_img_url'];
                $result[$k]['action_img_url'] = getPicDomain() . $v['action_img_url'];
            }
              //Redis::HSET($this->cacheSystemFullScreenHash, $v['id'], json_encode($v));
        }
         //Redis::EXPIRE($this->cacheSystemFullScreenHash, 3600);

        return $result;
    }


    private function cleanCache($field=null)
    {
        $key_name = $this->cacheSystemFullScreenHash;
        if ($field){
            Redis::Hdel($key_name, $field);
        }else{
            Redis::DEL($key_name);
        }
    }






}
